import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.xml.bind.Element;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) {
        test01();
    }

    private static void test12() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();

        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        //转换为“截图”对象。获取的截图在“内存”中
        File file = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        //将截图对象从内存放到硬盘中，地址为......
        FileUtils.copyFile(file, new File("D://20230521jietu.png"));
    }

    private static void test11() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);
        // 通过getWindowHandles获取所有的窗口句柄
        // 通过getWindowHandle获取get打开的页面窗口句柄（这里就是百度主页面）
        System.out.println(webDriver.getWindowHandle());
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle = "";
        for(String handle:handles) {
            //拿到最后一个handle元素
            target_handle = handle;
        }
        //切换到最后一个窗口，然后再这个窗口上寻找目标元素
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();

    }

    private static void test10() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(4000);
//        webDriver.quit();
        webDriver.close();
    }

    private static void page05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test05.html?_ijt=7asvni2vctahrdl90k8v4lvq5&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("input")).sendKeys("D:\\untitled");
    }

    private static void page04() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test04.html?_ijt=u8fai3bvc2dvsjigccqcapieg1&_ij_reload=RELOAD_ON_SAVE");
        //点击跳出弹窗
        webDriver.findElement(By.cssSelector("button")).click();
        sleep(3000);
        // alert弹窗取消
        webDriver.switchTo().alert().dismiss();
        sleep(3000);
        // 点击按钮
        webDriver.findElement(By.cssSelector("button")).click();
        // 在alert弹窗中输入沙天琦
        webDriver.switchTo().alert().sendKeys("沙天琦");
        // alert弹窗确认
        sleep(3000);
        //switchTo()意思是“切换状态”？
        webDriver.switchTo().alert().accept();
    }

    private static void page03() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test03.html?_ijt=qje1h6c6groepeefv0ae9bnla2&_ij_reload=RELOAD_ON_SAVE");
        WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(webElement);
//        select.selectByIndex(3);
        select.selectByValue("12.51");
    }

    private static void page02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test02.html?_ijt=arpr09o5r3gegeidj4o2r6hc9b&_ij_reload=RELOAD_ON_SAVE");
        //先定位到frame的id = f1的框架中，然后再去找其中的元素
        webDriver.switchTo().frame("f1");
        //如果直接选中的话无法选择到该元素
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/_20230512testcode/src/main/Page/test01.html?_ijt=4brheilpuaaaoa465u843m3sth&_ij_reload=RELOAD_ON_SAVE");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for(int i = 0; i < webElements.size(); i++) {
            // 如果每个元素type值等于checkbox进行点击
            // getAttribute获取页面上的元素属性值，里面的type是当前元素属性
            if(webElements.get(i).getAttribute("type").equals("checkbox")){
                webElements.get(i).click();
            } else {
                // 否则什么也不操作
                ;
            }
        }
    }

    private static void test09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 找到图片按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        // 鼠标右击
        Actions actions = new Actions(webDriver);
        sleep(3000);
        //将鼠标移动到webElement元素并右击鼠标（perform用于展示操作结果，否则会看不见）
        actions.moveToElement(webElement).contextClick().perform();
    }

    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://wwww.baidu.com/");
        // 搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        // control+A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        sleep(3000);
        // control+X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        sleep(3000);
        // control+V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
        sleep(3000);
    }

    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://wwww.baidu.com/");
        // 搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        /*搜索输入框id是“kw”，“百度一下”是“su”*/
        // 强制等待3秒
        webDriver.findElement(By.cssSelector("#su")).click();
        // 浏览器后退
        sleep(3000);
        webDriver.navigate().back();
        sleep(3000);
        //浏览器刷新
        webDriver.navigate().refresh();
        sleep(3000);
        // 浏览器前进
        webDriver.navigate().forward();
        //浏览器滚动10000像素（向下滚动）
        sleep(3000);
        //如果没有达到预期效果，一般是因为代码执行太快
        //页面没有渲染好
        //将webDriver强制转化为JS格式，用于执行JS命令
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        //浏览器最大化
        webDriver.manage().window().maximize();
        sleep(3000);
        //浏览器全屏
        webDriver.manage().window().fullscreen();
        sleep(3000);
        //调整浏览器大小
        webDriver.manage().window().setSize(new Dimension(600, 1000));
    }

    private static void test06() {
        //如果使用的驱动是3.版本的话，可以直接创建对象，
        // 不用写“ChromeOptions options = new ChromeOptions();”之类的语句
        // 创建驱动
        WebDriver webDriver = new ChromeDriver();
        // 打开百度首页
        webDriver.get("https://www.baidu.com/");
        // 判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver, 1);//第一个参数是驱动，第二个参数是等待时间
//        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div111 ")));
        //上述语句意思是：“持续等待，直到选择器中的元素可以被点击操作”
        //这里是故意把这个标签名字写错了，用于检验——找不到该标签时的报错情况
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
        //校验标签title是否为引号中的内容

    }

    private static void test05() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if(url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("当前页面url：" + url + "，当前页面title：" + title);
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }

    private static void test04() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
//        System.out.println(button_value);
        if(button_value.equals("百度一下")) {
            System.out.println("测试通过");
        } else {
            System.out.println(button_value);
            System.out.println("测试不通过");
        }
    }

    private static void test03() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).submit();
    }

    private static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        sleep(3000);
        // 找到百度搜索输入框，输入“java107”
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("Java107");
        // 点击了百度一下按钮
        webDriver.findElement(By.cssSelector("#su")).submit();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        // 清空百度搜索输入框中的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void test01() throws InterruptedException {
        int flag = 0;
        ChromeOptions options = new ChromeOptions();
        // 允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        // 打开百度首页
        webDriver.get("https://www.baidu.com");
        // 找到百度搜索输入框
        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
        //WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        // 输入软件测试
        element.sendKeys("软件测试");
        // 找到百度一下按钮
        // 点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);
        // 校验
        // 找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for(int i = 0; i < elements.size(); i++) {
            // 如果返回的结果有软件测试，证明测试通过，否则测试不通过
            if(elements.get(i).getText().equals("测试")) {
                flag = 1;
                System.out.println("测试通过");
                break;
            }
        }
        if(flag == 0) {
            System.out.println("测试不通过");
        }
    }
}
